<template>
  <view style='position: relative;z-index: 1100;'>
    <uv-action-sheet ref="actionSheet" :actions="list" cancelText='取消' closeOnClickOverlay safeAreaInsetBottom @select="select"></uv-action-sheet>
    
    <uv-modal ref="modal" showCancelButton confirmColor='#E43222' title="提示" @confirm="confirm">
      <view style="text-align: center;">需要您的同意,才能访问{{ msg }}</view>
    </uv-modal>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { setUpload } from '@/common';
import { judgeIosPermission, gotoAppPermissionSetting } from "@/common/permission.js";

const emit = defineEmits(['click']);
const props = defineProps({
  type: {
    type: Array,
    default: () => []
  },
  size: {
    type: Object,
    default: () => {}
  }
});
const actionSheet = ref(null);
const modal = ref(null);
const os = uni.$uv.os();
const list = [
  { name: '拍照', value: '拍照功能', type: 'camera', index: 1 },
  { name: '从相册选择', value: '相册功能', type: 'album', index: 2 }
]
let msg = ref('');

function select(e){
  msg.value = e.value;
  uni.chooseImage({
  	count: 1,
  	sourceType: [e.type], //从相册选择
    crop: props.size,
  	success: function (res) {
      const tempFilePaths = res.tempFilePaths;
      setUpload(tempFilePaths).then(url => {
        emit('click', url);
      });
  	},
    fail(err) {
      console.log(err);
      if(e.index == 1 && err.code == 2){
        modal.value.open();
      }
      // if(e.index == 2 && err.code == 8){
      //   modal.value.open();
      // }
      if(err.errMsg == 'chooseImage:fail No Permission' && (err.code == 11 || err.code == 12)){
        modal.value.open();
      }
    }
  });
}
function confirm(){
  gotoAppPermissionSetting()
}
function open(){
  actionSheet.value.open();
}
function close(){
  actionSheet.value.close();
}

defineExpose({
  open,
  select
})
</script>

<style>
</style>